1
|
|
|
var GedcomX = require('../'), |
2
|
|
|
utils = require('../utils'); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Someone or something that curates genealogical data, such as a genealogical |
6
|
|
|
* researcher, user of software, or organization. |
7
|
|
|
* |
8
|
|
|
* @constructor |
9
|
|
|
* @param {Object} [json] |
|
|
|
|
10
|
|
|
*/ |
11
|
|
|
var Agent = function(json){ |
12
|
|
|
|
13
|
|
|
// Protect against forgetting the new keyword when calling the constructor |
14
|
|
|
if(!(this instanceof Agent)){ |
15
|
|
|
return new Agent(json); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
// If the given object is already an instance then just return it. DON'T copy it. |
19
|
|
|
if(Agent.isInstance(json)){ |
20
|
|
|
return json; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
this.init(json); |
|
|
|
|
24
|
|
|
}; |
25
|
|
|
|
26
|
|
|
Agent.prototype = Object.create(GedcomX.ExtensibleData.prototype); |
27
|
|
|
|
28
|
|
|
Agent._gedxClass = Agent.prototype._gedxClass = 'GedcomX.Agent'; |
29
|
|
|
|
30
|
|
|
Agent.jsonProps = [ |
31
|
|
|
'identifiers', |
32
|
|
|
'names', |
33
|
|
|
'homepage', |
34
|
|
|
'openid', |
35
|
|
|
'accounts', |
36
|
|
|
'emails', |
37
|
|
|
'phones', |
38
|
|
|
'addresses', |
39
|
|
|
'person' |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Check whether the given object is an instance of this class. |
44
|
|
|
* |
45
|
|
|
* @param {Object} obj |
46
|
|
|
* @returns {Boolean} |
47
|
|
|
*/ |
48
|
|
|
Agent.isInstance = function(obj){ |
49
|
|
|
return utils.isInstance(obj, this._gedxClass); |
50
|
|
|
}; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Initialize from JSON |
54
|
|
|
* |
55
|
|
|
* @param {Object} |
|
|
|
|
56
|
|
|
* @return {Agent} this |
57
|
|
|
*/ |
58
|
|
|
Agent.prototype.init = function(json){ |
59
|
|
|
|
60
|
|
|
GedcomX.ExtensibleData.prototype.init.call(this, json); |
61
|
|
|
|
62
|
|
|
if(json){ |
63
|
|
|
this.setIdentifiers(json.identifiers); |
64
|
|
|
this.setNames(json.names); |
65
|
|
|
this.setHomepage(json.homepage); |
66
|
|
|
this.setOpenid(json.openid); |
67
|
|
|
this.setAccounts(json.accounts); |
68
|
|
|
this.setEmails(json.emails); |
69
|
|
|
this.setPhones(json.phones); |
70
|
|
|
this.setAddresses(json.addresses); |
71
|
|
|
this.setPerson(json.person); |
72
|
|
|
} |
73
|
|
|
return this; |
74
|
|
|
}; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get the identifiers |
78
|
|
|
* |
79
|
|
|
* @returns {Identifiers} |
80
|
|
|
*/ |
81
|
|
|
Agent.prototype.getIdentifiers = function(){ |
82
|
|
|
return this.identifiers; |
83
|
|
|
}; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Set the identifiers |
87
|
|
|
* |
88
|
|
|
* @param {Identifiers} identifiers |
89
|
|
|
* @returns {Agent} |
90
|
|
|
*/ |
91
|
|
|
Agent.prototype.setIdentifiers = function(identifiers){ |
92
|
|
|
if(identifiers){ |
93
|
|
|
this.identifiers = GedcomX.Identifiers(identifiers); |
94
|
|
|
} |
95
|
|
|
return this; |
96
|
|
|
}; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get the names |
100
|
|
|
* |
101
|
|
|
* @returns {TextValue[]} |
102
|
|
|
*/ |
103
|
|
|
Agent.prototype.getNames = function(){ |
104
|
|
|
return this.names || []; |
105
|
|
|
}; |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Set the names |
109
|
|
|
* |
110
|
|
|
* @param {TextValue[]|Object[]} names |
111
|
|
|
* @returns {Agent} |
112
|
|
|
*/ |
113
|
|
|
Agent.prototype.setNames = function(names){ |
114
|
|
|
return this._setArray(names, 'names', 'addName'); |
115
|
|
|
}; |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Add a name |
119
|
|
|
* |
120
|
|
|
* @param {TextValue|Object} name |
121
|
|
|
* @return {Agent} |
122
|
|
|
*/ |
123
|
|
|
Agent.prototype.addName = function(name){ |
124
|
|
|
return this._arrayPush(name, 'names', GedcomX.TextValue); |
125
|
|
|
}; |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get the home page |
129
|
|
|
* |
130
|
|
|
* @returns {ResourceReference} |
131
|
|
|
*/ |
132
|
|
|
Agent.prototype.getHomepage = function(){ |
133
|
|
|
return this.homepage; |
134
|
|
|
}; |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Set the home page |
138
|
|
|
* |
139
|
|
|
* @param {ResourceReference|Object} homepage |
140
|
|
|
* @returns {Agent} |
141
|
|
|
*/ |
142
|
|
|
Agent.prototype.setHomepage = function(homepage){ |
143
|
|
|
if(homepage){ |
144
|
|
|
this.homepage = GedcomX.ResourceReference(homepage); |
145
|
|
|
} |
146
|
|
|
return this; |
147
|
|
|
}; |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get the openid |
151
|
|
|
* |
152
|
|
|
* @returns {ResourceReference} |
153
|
|
|
*/ |
154
|
|
|
Agent.prototype.getOpenid = function(){ |
155
|
|
|
return this.openid; |
156
|
|
|
}; |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Set the openid |
160
|
|
|
* |
161
|
|
|
* @params {ResourceReference} openid |
162
|
|
|
* @returns {Agent} |
163
|
|
|
*/ |
164
|
|
|
Agent.prototype.setOpenid = function(openid){ |
165
|
|
|
if(openid){ |
166
|
|
|
this.openid = GedcomX.ResourceReference(openid); |
167
|
|
|
} |
168
|
|
|
return this; |
169
|
|
|
}; |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Get the accounts |
173
|
|
|
* |
174
|
|
|
* @returns {OnlineAccount[]} |
175
|
|
|
*/ |
176
|
|
|
Agent.prototype.getAccounts = function(){ |
177
|
|
|
return this.accounts || []; |
178
|
|
|
}; |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Set the accounts |
182
|
|
|
* |
183
|
|
|
* @param {OnlineAccount[]|Object[]} accounts |
184
|
|
|
* @returns {Agent} |
185
|
|
|
*/ |
186
|
|
|
Agent.prototype.setAccounts = function(accounts){ |
187
|
|
|
return this._setArray(accounts, 'accounts', 'addAccount'); |
188
|
|
|
}; |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Add an account |
192
|
|
|
* |
193
|
|
|
* @param {OnlineAccount|Object} account |
194
|
|
|
* @returns {Agent} |
195
|
|
|
*/ |
196
|
|
|
Agent.prototype.addAccount = function(account){ |
197
|
|
|
return this._arrayPush(account, 'accounts', GedcomX.OnlineAccount); |
198
|
|
|
}; |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Get the emails |
202
|
|
|
* |
203
|
|
|
* @returns {ResourceReference[]} |
204
|
|
|
*/ |
205
|
|
|
Agent.prototype.getEmails= function(){ |
206
|
|
|
return this.emails || []; |
207
|
|
|
}; |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Set the emails |
211
|
|
|
* |
212
|
|
|
* @param {ResourceReference[]|Object[]} emails |
213
|
|
|
* @returns {Agent} |
214
|
|
|
*/ |
215
|
|
|
Agent.prototype.setEmails = function(emails){ |
216
|
|
|
return this._setArray(emails, 'emails', 'addEmail'); |
217
|
|
|
}; |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Add an email |
221
|
|
|
* |
222
|
|
|
* @param {ResourceReference|Object} email |
223
|
|
|
* @returns {Agent} |
224
|
|
|
*/ |
225
|
|
|
Agent.prototype.addEmail = function(email){ |
226
|
|
|
return this._arrayPush(email, 'emails', GedcomX.ResourceReference); |
227
|
|
|
}; |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Get the phones |
231
|
|
|
* |
232
|
|
|
* @returns {ResourceReference[]} |
233
|
|
|
*/ |
234
|
|
|
Agent.prototype.getPhones = function(){ |
235
|
|
|
return this.phones || []; |
236
|
|
|
}; |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Set the phones |
240
|
|
|
* |
241
|
|
|
* @param {ResourceReference[]|Object[]} phones |
242
|
|
|
* @returns {Agent} |
243
|
|
|
*/ |
244
|
|
|
Agent.prototype.setPhones = function(phones){ |
245
|
|
|
return this._setArray(phones, 'phones', 'addPhone'); |
246
|
|
|
}; |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Add a phone |
250
|
|
|
* |
251
|
|
|
* @param {ResourceReference|Object} phone |
252
|
|
|
* @returns {Agent} |
253
|
|
|
*/ |
254
|
|
|
Agent.prototype.addPhone = function(phone){ |
255
|
|
|
return this._arrayPush(phone, 'phones', GedcomX.ResourceReference); |
256
|
|
|
}; |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Get the addresses |
260
|
|
|
* |
261
|
|
|
* @returns {Address[]} |
262
|
|
|
*/ |
263
|
|
|
Agent.prototype.getAddresses = function(){ |
264
|
|
|
return this.addresses || []; |
265
|
|
|
}; |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Set the addresses |
269
|
|
|
* |
270
|
|
|
* @param {Address[]|Object[]} addresses |
271
|
|
|
* @returns {Agent} |
272
|
|
|
*/ |
273
|
|
|
Agent.prototype.setAddresses = function(addresses){ |
274
|
|
|
return this._setArray(addresses, 'addresses', 'addAddress'); |
275
|
|
|
}; |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Add an address |
279
|
|
|
* |
280
|
|
|
* @param {Address|Object} address |
281
|
|
|
* @returns {Agent} |
282
|
|
|
*/ |
283
|
|
|
Agent.prototype.addAddress = function(address){ |
284
|
|
|
return this._arrayPush(address, 'addresses', GedcomX.Address); |
285
|
|
|
}; |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Get the person reference |
289
|
|
|
* |
290
|
|
|
* @returns {ResourceReference} |
291
|
|
|
*/ |
292
|
|
|
Agent.prototype.getPerson = function(){ |
293
|
|
|
return this.person; |
294
|
|
|
}; |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Set the person reference |
298
|
|
|
* |
299
|
|
|
* @param {ResourceReference} person |
300
|
|
|
* @returns {Agent} |
301
|
|
|
*/ |
302
|
|
|
Agent.prototype.setPerson = function(person){ |
303
|
|
|
if(person){ |
304
|
|
|
this.person = GedcomX.ResourceReference(person); |
305
|
|
|
} |
306
|
|
|
return this; |
307
|
|
|
}; |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Export the object as JSON |
311
|
|
|
* |
312
|
|
|
* @return {Object} JSON object |
313
|
|
|
*/ |
314
|
|
|
Agent.prototype.toJSON = function(){ |
315
|
|
|
return this._toJSON(GedcomX.ExtensibleData, Agent.jsonProps); |
316
|
|
|
}; |
317
|
|
|
|
318
|
|
|
module.exports = Agent; |